package com.landiao.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.landiao.common.Constant;
import com.landiao.common.Constant.PayResult;
import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.EmpOrderInfo;
import com.landiao.common.pojo.Page;
import com.landiao.common.pojo.Querier;
import com.landiao.common.pojo.ResultMsg;
import com.landiao.common.utils.CodecUtils;
import com.landiao.common.utils.DateUtil;
import com.landiao.common.weixin.WxMpService;
import com.landiao.common.weixin.bean.WxMpTemplateData;
import com.landiao.common.weixin.bean.WxMpTemplateMessage;
import com.landiao.entities.CouponReceive;
import com.landiao.entities.EmployeeScore;
import com.landiao.entities.MdseBuyRecord;
import com.landiao.entities.MdseStore;
import com.landiao.entities.OrderDetail;
import com.landiao.entities.OrderInfo;
import com.landiao.entities.OrderSchedule;
import com.landiao.entities.PayFlow;
import com.landiao.entities.SendTemplate;
import com.landiao.entities.ServiceItemRoleRela;
import com.landiao.entities.Vip;
import com.landiao.mapper.CouponReceiveMapper;
import com.landiao.mapper.EmployeeScoreMapper;
import com.landiao.mapper.OrderDetailMapper;
import com.landiao.mapper.OrderInfoMapper;
import com.landiao.mapper.OrderScheduleMapper;
import com.landiao.mapper.PayFlowMapper;
import com.landiao.mapper.VipMapper;

/**
 * 功能说明:
 *
 */
@Service
public class OrderInfoService extends BaseService<OrderInfo, String> {

	@Value("${weixin.site}")
	protected String webSite;

	@Autowired
	@Qualifier("vipWxService")
	private WxMpService vipWxService;

	@Autowired
	private OrderDetailMapper detailMapper;

	@Autowired
	private OrderScheduleMapper scheduleMapper;

	@Autowired
	private EmployeeScoreMapper scoreMapper;

	@Autowired
	private SysConfigService sysConfigService;

	@Autowired
	private ServiceItemService itemService;

	@Autowired
	private CouponReceiveMapper receiveMapper;

	@Autowired
	private VipMapper vipMapper;

	@Autowired
	private PayFlowMapper payFlowMapper;

	@Autowired
	private MdseStoreService mdseStoreService;
	
	@Autowired
	private MdseBuyRecordService mdseBuyRecordService;

	@Override
	protected OrderInfoMapper getBaseMapper() {
		// TODO Auto-generated method stub
		return (OrderInfoMapper)baseMapper;
	}

	public OrderInfo getByOrderNo(String orderNo) {
		return this.getBaseMapper().selectByOrderNo(orderNo);
	}
    public void updateSelective(OrderInfo orderInfo) {
    	this.getBaseMapper().updateByPrimaryKeySelective(orderInfo);
    }
	/**
	 * 分页查询
	 *
	 */
	public Page<OrderInfo> queryByPageFromWx(Querier querier,OrderInfo t) {
		Page<OrderInfo> page = new Page<OrderInfo>(querier);
		List<OrderInfo> list = getBaseMapper().queryByPageFromWx(t, page);
		page.setRows(list);
		return page;
	}

	/**
	 * 分页查询
	 *
	 */
	public Page<EmpOrderInfo> queryEmpOrderByPage(Querier querier,String employeeId) {
		Page<EmpOrderInfo> page = new Page<EmpOrderInfo>(querier);
		List<EmpOrderInfo> list = getBaseMapper().queryEmpOrderByPage(employeeId, page);
		page.setRows(list);
		return page;
	}

	public List<OrderDetail> getDetails(String orderInfoId){
		OrderDetail od = new OrderDetail();
		od.setOrderInfoId(orderInfoId);
		OrderSchedule os = new OrderSchedule();
		os.setOrderInfoId(orderInfoId);
		List<OrderDetail> details = this.detailMapper.queryByPage(od, null);
		List<OrderSchedule> sches = this.scheduleMapper.queryByPage(os, null);
		Map<String, List<OrderSchedule>> map = new HashMap<>();
		for(OrderSchedule sche:sches){
			List<OrderSchedule> tmpList= map.get(sche.getOrderDetailId());
			if(tmpList==null){
				tmpList = new ArrayList<OrderSchedule>();
				map.put(sche.getOrderDetailId(), tmpList);
			}
			tmpList.add(sche);
		}
		for(OrderDetail detail:details){
			detail.setScheList(map.get(detail.getOrderDetailId()));
		}

		return details;
	}

	public OrderInfo getWithDetail(String orderInfoId){
		OrderInfo orderInfo = this.getBaseMapper().selectByPrimaryKey(orderInfoId);
		if(orderInfo!=null){
			OrderDetail od = new OrderDetail();
			od.setOrderInfoId(orderInfoId);
			OrderSchedule os = new OrderSchedule();
			os.setOrderInfoId(orderInfoId);
			List<OrderDetail> details = this.detailMapper.queryByPage(od, null);
			List<OrderSchedule> sches = this.scheduleMapper.queryByPage(os, null);
			Map<String, List<OrderSchedule>> map = new HashMap<>();
			//    		Map<String, String> statemap = new HashMap<>();
			for(OrderSchedule sche:sches){
				List<OrderSchedule> tmpList= map.get(sche.getOrderDetailId());
				if(tmpList==null){
					tmpList = new ArrayList<OrderSchedule>();
					map.put(sche.getOrderDetailId(), tmpList);
				}
				tmpList.add(sche);
				//    			if("10F".equals(sche.getState())){//已服务
				//    				statemap.put(sche.getOrderDetailId(), sche.getState());
				//    			}
			}
			for(OrderDetail detail:details){
				detail.setScheList(map.get(detail.getOrderDetailId()));
				//    			String state = statemap.get(detail.getOrderDetailId());
				//    			if(state!=null){
				//    				detail.setState(state);//已服务
				//    			}
			}
			orderInfo.setDetailList(details);
		}

		return orderInfo;
	}

	/**
	 * 保存订单评价
	 */
	public void saveOrderEvaluate(OrderInfo orderInfo){
		orderInfo.setIsEvaluate("10S");
		orderInfo.setStatedate(new Date());
		this.getBaseMapper().updateByPrimaryKeySelective(orderInfo);
		if(orderInfo.getScoreList()!=null){
			for(EmployeeScore score:orderInfo.getScoreList()){
				if(score.getEmployeeId()!=null&&StringUtils.isNotBlank(score.getEmployeeId())&&!score.getEmployeeId().equals("null")){
					score.setCompId(orderInfo.getCompId());
					score.setEmployeeScoreId(CodecUtils.getUUID());
					this.scoreMapper.insert(score);
				}
			}
		}
	}

	/**
	 * 保存订单
	 * @param orderInfo
	 */
	public void saveOrder(OrderInfo orderInfo){
		BigDecimal percent = new BigDecimal("100");
		BigDecimal mdsePercent = new BigDecimal(sysConfigService.getSysConfigMap().get(Constant.SysConfigItemEnum.MDSE_PERCENT.toString()).getVal1()).divide(percent);

		Date nowDate = new Date();
		orderInfo.setOrderInfoId(CodecUtils.getUUID());
		orderInfo.setOrderNo(CodecUtils.getOrderNo());
		orderInfo.setCreTime(nowDate);
		orderInfo.setIsEvaluate("10A");
		orderInfo.setPayState("10X");
		orderInfo.setState(Constant.ORDER_STATE_NOT_OVER);
		orderInfo.setStatedate(nowDate);
		BigDecimal amount = new BigDecimal("0");
		List<OrderSchedule> scheList = new ArrayList<>();
		for(OrderDetail detail:orderInfo.getDetailList()){
			detail.setOrderDetailId(CodecUtils.getUUID());
			detail.setOrderInfoId(orderInfo.getOrderInfoId());
			detail.setCompId(orderInfo.getCompId());
			detail.setShopId(orderInfo.getShopId());
			detail.setVipId(orderInfo.getVipId());
			detail.setState("10A");
			detail.setStatedate(nowDate);
			detail.setAmount(detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString())));

			if(detail.getMdseId()!=null){
				BigDecimal mdsePrice = detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString()));

				OrderSchedule sche = new OrderSchedule();
				sche.setOrderScheduleId(CodecUtils.getUUID());
				sche.setCompId(orderInfo.getCompId());
				sche.setOrderDetailId(detail.getOrderDetailId());
				sche.setOrderInfoId(orderInfo.getOrderInfoId());
				sche.setEmployeeId(orderInfo.getEmployeeId());
				sche.setShopId(orderInfo.getShopId());
				sche.setVipId(orderInfo.getVipId());
				sche.setBaseAmount(mdsePrice);
				sche.setCommisionAmount(mdsePrice.multiply(mdsePercent));
				sche.setIsCash("10A");
				sche.setIsDk("10X");
				sche.setIsBalance("10X");
				sche.setState("10F");
				sche.setStatedate(nowDate);

				detail.setScheList(new ArrayList<OrderSchedule>());
				detail.getScheList().add(sche);
				detail.setState("10F");
				amount = amount.add(mdsePrice);

				scheList.add(sche);
			}else{
				amount = amount.add(detail.getPrice());
				List<ServiceItemRoleRela> roles = this.itemService.getRoleRelaByServiceItemId(detail.getServiceItemId());
				for(OrderSchedule sche:detail.getScheList()){
					sche.setOrderScheduleId(CodecUtils.getUUID());
					sche.setCompId(orderInfo.getCompId());
					sche.setOrderDetailId(detail.getOrderDetailId());
					sche.setOrderInfoId(orderInfo.getOrderInfoId());
					sche.setShopId(orderInfo.getShopId());
					sche.setVipId(orderInfo.getVipId());
					sche.setBaseAmount(detail.getPrice());
					if(!sche.getRoleId().equals(Constant.RoleIdEnum.FXS.toString())){
						for(ServiceItemRoleRela roleRela:roles){
							if(sche.getRoleId().equals(roleRela.getRoleId())){
								if(roleRela.getCommisionAmount()!=null){
									sche.setCommisionAmount(roleRela.getCommisionAmount());
								}else{
									sche.setCommisionAmount(detail.getPrice().multiply(roleRela.getCommisionRatio().divide(percent)));
								}
								break;
							}
						}
					}
					sche.setIsCash("10A");
					sche.setIsDk("10X");
					sche.setIsBalance("10X");
					sche.setState("10A");
					sche.setStatedate(nowDate);

					scheList.add(sche);
				}
			}
		}
		orderInfo.setAmount(amount);
		//    	orderInfo.setShouldPay(amount);
		this.getBaseMapper().insert(orderInfo);
		this.detailMapper.batchInsert(orderInfo.getDetailList());
		this.scheduleMapper.batchInsert(scheList);
		//    	for(OrderDetail detail:orderInfo.getDetailList()){
		//    		this.detailMapper.insert(detail);
		//    		for(OrderSchedule sche:detail.getScheList()){
		//    			this.scheduleMapper.insert(sche);
		//    		}
		//    	}
	}

	/**
	 * 
	 * @param orderInfo
	 * @param orderScheduleId 页面下一步指定完成的流程ID
	 */
	public void updateOrder(OrderInfo orderInfo,String orderScheduleId,String employeeId){
		BigDecimal percent = new BigDecimal("100");
		BigDecimal mdsePercent = new BigDecimal(sysConfigService.getSysConfigMap().get(Constant.SysConfigItemEnum.MDSE_PERCENT.toString()).getVal1()).divide(percent);

		Date nowDate = new Date();
		List<OrderSchedule> scheList = new ArrayList<>();
		List<OrderDetail> detailList = new ArrayList<>();
		for(OrderDetail detail:orderInfo.getDetailList()){
			detail.setAmount(detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString())));
			if(StringUtils.isNotEmpty(detail.getOrderDetailId())){
				if(detail.getMdseId()!=null){
					if(orderInfo.getPayState().equals("10X")){
						OrderSchedule sche = detail.getScheList().get(0);
						BigDecimal mdsePrice = detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString()));
						if(mdsePrice.compareTo(sche.getBaseAmount())!=0){//不=，说明有变化
							detail.setStatedate(nowDate);
							this.detailMapper.updateByPrimaryKeySelective(detail);
							sche.setBaseAmount(mdsePrice);
							sche.setCommisionAmount(mdsePrice.multiply(mdsePercent));
							sche.setStatedate(nowDate);
							this.scheduleMapper.updateByPrimaryKeySelective(sche);
						}
					}
				}else{
					boolean hasUnOver = false;
					boolean hasOver = false;
					for(OrderSchedule sche:detail.getScheList()){
						if(sche.getState().equals("10A")){//未完成，保存
							if(sche.getOrderScheduleId().equals(orderScheduleId)){//下一步
								sche.setState("10F");
								sche.setEmployeeId(employeeId);
								sche.setStatedate(nowDate);
								this.scheduleMapper.updateByPrimaryKeySelective(sche);
								hasOver = true;
							}else{
								this.scheduleMapper.updateByPrimaryKeySelectiveWithEmployee(sche);
								hasUnOver=true;
							}
						}else{
							hasOver = true;
						}
					}
					String state = "10A";
					if(hasUnOver){
						if(hasOver)
							state="10I";//服务中
					}else{
						state="10F";//已完成
					}
					if(!state.equals(detail.getState())){
						detail.setState(state);
						detail.setStatedate(nowDate);
						this.detailMapper.updateByPrimaryKeySelective(detail);
					}
				}
			}else{
				if(orderInfo.getPayState().equals("10X")){
					detail.setOrderDetailId(CodecUtils.getUUID());
					detail.setOrderInfoId(orderInfo.getOrderInfoId());
					detail.setCompId(orderInfo.getCompId());
					detail.setShopId(orderInfo.getShopId());
					detail.setVipId(orderInfo.getVipId());
					detail.setState("10A");
					detail.setStatedate(nowDate);
					detailList.add(detail);

					if(detail.getMdseId()!=null){
						BigDecimal mdsePrice = detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString()));

						OrderSchedule sche = new OrderSchedule();
						sche.setOrderScheduleId(CodecUtils.getUUID());
						sche.setCompId(orderInfo.getCompId());
						sche.setOrderDetailId(detail.getOrderDetailId());
						sche.setOrderInfoId(orderInfo.getOrderInfoId());
						sche.setEmployeeId(employeeId);
						sche.setShopId(orderInfo.getShopId());
						sche.setVipId(orderInfo.getVipId());
						sche.setBaseAmount(mdsePrice);
						sche.setCommisionAmount(mdsePrice.multiply(mdsePercent));
						sche.setIsCash("10A");
						sche.setIsDk("10X");
						sche.setIsBalance("10X");
						sche.setState("10F");
						sche.setStatedate(nowDate);

						detail.setScheList(new ArrayList<OrderSchedule>());
						detail.getScheList().add(sche);
						detail.setState("10F");

						scheList.add(sche);
					}else{
						List<ServiceItemRoleRela> roles = this.itemService.getRoleRelaByServiceItemId(detail.getServiceItemId());
						for(OrderSchedule sche:detail.getScheList()){
							sche.setOrderScheduleId(CodecUtils.getUUID());
							sche.setCompId(orderInfo.getCompId());
							sche.setOrderDetailId(detail.getOrderDetailId());
							sche.setOrderInfoId(orderInfo.getOrderInfoId());
							sche.setShopId(orderInfo.getShopId());
							sche.setVipId(orderInfo.getVipId());
							sche.setBaseAmount(detail.getPrice());
							if(!sche.getRoleId().equals(Constant.RoleIdEnum.FXS.toString())){
								for(ServiceItemRoleRela roleRela:roles){
									if(sche.getRoleId().equals(roleRela.getRoleId())){
										if(roleRela.getCommisionAmount()!=null){
											sche.setCommisionAmount(roleRela.getCommisionAmount());
										}else{
											sche.setCommisionAmount(detail.getPrice().multiply(roleRela.getCommisionRatio().divide(percent)));
										}
										break;
									}
								}
							}
							sche.setIsCash("10A");
							sche.setIsDk("10X");
							sche.setIsBalance("10X");
							sche.setState("10A");
							sche.setStatedate(nowDate);

							scheList.add(sche);
						}
					}
				}
			}
		}

		if(detailList.size()>0){
			this.detailMapper.batchInsert(detailList);
		}
		if(scheList.size()>0){
			this.scheduleMapper.batchInsert(scheList);
		}
		
		BigDecimal amount = new BigDecimal("0");
		for(OrderDetail detail:orderInfo.getDetailList()){
			if(detail.getMdseId()!=null){
				BigDecimal mdsePrice = detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString()));
				amount = amount.add(mdsePrice);
			}else{
				amount = amount.add(detail.getPrice());
			}
		}
		orderInfo.setAmount(amount.setScale(1, BigDecimal.ROUND_HALF_UP));
		this.baseMapper.updateByPrimaryKeySelective(orderInfo);
	}

	public List<OrderDetail> getHasService(List<String> detailIds){
		return this.detailMapper.selectHasService(detailIds);
	}

	public void batchDeleteDetails(List<String> detailIds){
		this.scheduleMapper.batchDelete(detailIds);
		this.detailMapper.batchDelete(detailIds);
	}

	public List<OrderDetail> getNotOverDetail(String orderInfoId){
		return this.detailMapper.selectNotOver(orderInfoId);
	}

	/**
	 * 现金、余额支付
	 * @param orderInfo
	 * @param type
	 * @param serviceItemId
	 * @return
	 */
	public ResultMsg saveOrderPay(OrderInfo orderInfo,final String payWay,String serviceItemId){
		BigDecimal percent = new BigDecimal("100");
		BigDecimal vipBalancePercent = new BigDecimal(sysConfigService.getSysConfigMap().get(Constant.SysConfigItemEnum.VIP_BALANCE_PERCENT.toString()).getVal1()).divide(percent);
		BigDecimal couponPercent = new BigDecimal(sysConfigService.getSysConfigMap().get(Constant.SysConfigItemEnum.COUPON_PERCENT.toString()).getVal1()).divide(percent);
		Date nowDate = new Date();
		CouponReceive cr = null;
		Vip vip = null;
		if(orderInfo.getVipId()!=null){
			vip = this.vipMapper.selectByPrimaryKey(orderInfo.getVipId());
		}
		BigDecimal dkAmount = new BigDecimal("0");
		if(StringUtils.isNotEmpty(serviceItemId)&&orderInfo.getPayState().equals("10X")){
			cr = this.receiveMapper.selectValidByVipIdAndServiceItemId(orderInfo.getVipId(), serviceItemId);
			//    		if(cr==null){
			//    			return ResultMsgFactory.fail("支付失败，代金券已过期或已使用");
			//    		}

			for(OrderDetail detail:orderInfo.getDetailList()){
				if(serviceItemId.equals(detail.getServiceItemId())&&detail.getPackageId()==null){
					for(OrderSchedule sche:detail.getScheList()){
						sche.setIsDk("10A");
					}
					dkAmount=detail.getPrice();
					break;
				}
			}
		}
		if(payWay.equals(Constant.PAY_WAY_YE)){
			if(vip!=null){
				BigDecimal amount = new BigDecimal("0");
				for(OrderDetail detail:orderInfo.getDetailList()){
					if(detail.getMdseId()!=null){
						BigDecimal mdsePrice = detail.getNoVipPrice().multiply(new BigDecimal(detail.getMdseNum().toString()));
						amount = amount.add(mdsePrice);
					}else{
						amount = amount.add(detail.getNoVipPrice());
					}
				}
				orderInfo.setAmount(amount);
				orderInfo.setShouldPay(amount.subtract(dkAmount));
				
				if(vip.getBalance().compareTo(orderInfo.getShouldPay())<0){
					return ResultMsgFactory.fail("支付失败，您当前余额不足");
				}
				vip.setBalance(vip.getBalance().subtract(orderInfo.getShouldPay()));
				
				this.updateOrderByYE(orderInfo);
			}else{
				return ResultMsgFactory.fail("支付失败，非会员不能使用余额支付");
			}
		}
		orderInfo.setShouldPay(orderInfo.getAmount().subtract(dkAmount));
		
		for(OrderDetail detail:orderInfo.getDetailList()){
			//detail.setAmount(detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString())));
			if(detail.getMdseId()!=null){
				List<MdseStore> msList = this.mdseStoreService.getMdseStoreByShopId(detail.getMdseId(), orderInfo.getShopId());
				if(msList!=null&&msList.size()>0){
					MdseStore ms = msList.get(0);
					ms.setNum(ms.getNum()-detail.getMdseNum());
					this.mdseStoreService.update(ms);
				}else{
					MdseStore ms = new MdseStore();
					ms.setCompId(orderInfo.getCompId());
					ms.setMdseStoreId(CodecUtils.getUUID());
					ms.setMdseId(detail.getMdseId());
					ms.setShopId(orderInfo.getShopId());
					ms.setNum(0-detail.getMdseNum());
					this.mdseStoreService.save(ms);
				}
				if(vip!=null){
					MdseBuyRecord buyRecord = new MdseBuyRecord();
					buyRecord.setMdseBuyRecordId(CodecUtils.getUUID());
					buyRecord.setCompId(orderInfo.getCompId());
					buyRecord.setVipId(vip.getVipId());
					buyRecord.setMdseId(detail.getMdseId());
					buyRecord.setBuyNum(detail.getMdseNum());
					buyRecord.setStatedate(nowDate);
					
					this.mdseBuyRecordService.save(buyRecord);
				}
				
				continue;
			}
			for(OrderSchedule sche:detail.getScheList()){
				boolean flag = false;
				if(payWay.equals(Constant.PAY_WAY_YE)){
					sche.setIsBalance("10A");
					flag = true;
				}
				if(!Constant.RoleIdEnum.FXS.toString().equals(sche.getRoleId())){
					if(sche.getIsDk().equals("10A")){
						flag = true;
						sche.setCommisionAmount(sche.getCommisionAmount().multiply(couponPercent));
					}
					if(sche.getIsBalance().equals("10A")){
						flag = true;
						sche.setCommisionAmount(sche.getCommisionAmount().multiply(vipBalancePercent));
					}
				}
				if(flag){
					this.scheduleMapper.updateByPrimaryKey(sche);
				}
			}
		}
		orderInfo.setPayState("10A");
		if(payWay.equals(Constant.PAY_WAY_XJ)){
			orderInfo.setState("10I");//现金支付中
		}else{
			orderInfo.setState("10S");
		}
		orderInfo.setPayWay(payWay);
		orderInfo.setStatedate(nowDate);
		this.getBaseMapper().updateByPrimaryKey(orderInfo);
		this.scheduleMapper.updateStatedateByOrderInfoId(orderInfo.getOrderInfoId());
		if(cr!=null){
			cr.setIsUsed("10X");
			cr.setStatedate(nowDate);
			this.receiveMapper.updateByPrimaryKey(cr);
		}
		if(vip!=null){
			vip.setPoints(vip.getPoints()+orderInfo.getShouldPay().intValue());
			this.vipMapper.updateByPrimaryKey(vip);
		}
		if(!payWay.equals(Constant.PAY_WAY_ZX)){
			PayFlow payFlow = new PayFlow();
			payFlow.setPayFlowId(CodecUtils.getUUID());
			payFlow.setCompId(orderInfo.getCompId());
			payFlow.setVipId(orderInfo.getVipId());
			payFlow.setFlowNo(orderInfo.getOrderNo());
			payFlow.setAmount(orderInfo.getShouldPay());
			payFlow.setPaySource(Constant.PAY_SOURCE_XF);
			payFlow.setPayWay(payWay);
			payFlow.setStatedate(nowDate);
			payFlow.setPrepayId(serviceItemId);
			payFlow.setCompleteTime(nowDate);
			payFlow.setPayResult(PayResult.SUCCESS.toString());
			payFlow.setTradeStateDesc("支付成功");
			this.payFlowMapper.insert(payFlow);
		}

		if(vip!=null&&vip.getOperId()!=null&&!payWay.equals(Constant.PAY_WAY_XJ)){
			final String operId = vip.getOperId();
			final String orderNo = orderInfo.getOrderNo();
			final String payAmount = orderInfo.getShouldPay().toString()+"元";
			new Thread(new Runnable(){
				public void run(){  
					try {
						SendTemplate temp = Constant.VIP_TEMP_MAP.get(Constant.TEMP_ID_PAY_SUCCESS);
						if(temp!=null){
							WxMpTemplateMessage message = new WxMpTemplateMessage();
							message.setUrl(webSite+"weixin/vip/myEvaluate");
							message.setTouser(operId);

							message.setTemplate_id(temp.getTemplateId());
							message.setTopcolor("#FF0000");

							message.getData().put("first", new WxMpTemplateData("您好，您的订单已经支付成功"));
							message.getData().put("keyword1", new WxMpTemplateData(orderNo));
							message.getData().put("keyword2", new WxMpTemplateData(DateUtil.date2Str(new Date(), "yyyy年M月d日 HH:mm")));
							message.getData().put("keyword3", new WxMpTemplateData(payAmount));
							if(payWay.equals(Constant.PAY_WAY_YE)){
								message.getData().put("keyword4", new WxMpTemplateData("余额支付"));
							}else if(payWay.equals(Constant.PAY_WAY_XJ)){
								message.getData().put("keyword4", new WxMpTemplateData("现金支付"));
							}else{
								message.getData().put("keyword4", new WxMpTemplateData("微信在线支付"));
							}
							message.getData().put("remark", new WxMpTemplateData("\n感谢您的惠顾，点击去评价"));

							vipWxService.templateSend(message);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}}).start();
		}
		return ResultMsgFactory.success("支付成功");
	}
	
	/**
	 * 余额结算,重新算提成
	 * @param orderInfo
	 * @param orderScheduleId 页面下一步指定完成的流程ID
	 */
	public void updateOrderByYE(OrderInfo orderInfo){
		BigDecimal percent = new BigDecimal("100");
		BigDecimal mdsePercent = new BigDecimal(sysConfigService.getSysConfigMap().get(Constant.SysConfigItemEnum.MDSE_PERCENT.toString()).getVal1()).divide(percent);
//		BigDecimal amount = new BigDecimal("0");
		
		for(OrderDetail detail:orderInfo.getDetailList()){
			detail.setPrice(detail.getNoVipPrice());
			detail.setAmount(detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString())));
			this.detailMapper.updateByPrimaryKeySelective(detail);
			
			if(detail.getMdseId()!=null){
				OrderSchedule sche = detail.getScheList().get(0);
				BigDecimal mdsePrice = detail.getPrice().multiply(new BigDecimal(detail.getMdseNum().toString()));
				sche.setBaseAmount(mdsePrice);
				sche.setCommisionAmount(mdsePrice.multiply(mdsePercent));
				this.scheduleMapper.updateByPrimaryKeySelective(sche);
//				amount = amount.add(mdsePrice);
			}else{
//				amount = amount.add(detail.getPrice());
				List<ServiceItemRoleRela> roles = this.itemService.getRoleRelaByServiceItemId(detail.getServiceItemId());
				for(OrderSchedule sche:detail.getScheList()){
					sche.setBaseAmount(detail.getPrice());
					if(!sche.getRoleId().equals(Constant.RoleIdEnum.FXS.toString())){
						for(ServiceItemRoleRela roleRela:roles){
							if(sche.getRoleId().equals(roleRela.getRoleId())){
								if(roleRela.getCommisionAmount()!=null){
									sche.setCommisionAmount(roleRela.getCommisionAmount());
								}else{
									sche.setCommisionAmount(detail.getPrice().multiply(roleRela.getCommisionRatio().divide(percent)));
								}
								break;
							}
						}
					}
					this.scheduleMapper.updateByPrimaryKeySelective(sche);
				}
			}
		}
//
//		orderInfo.setAmount(amount.setScale(1, BigDecimal.ROUND_HALF_UP));
//		this.baseMapper.updateByPrimaryKeySelective(orderInfo);
	}
}
